package com.example.uchain.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import okhttp3.*;

import java.io.IOException;
import java.util.List;

/**
 * 智能合约封装交互工具类
 */
public class HttpUtils {
    private static final String contractAddress = "0xf87e18eb0bfd7ee1a725c52e8dd7c0f39a88b02f";
    private static final String contractName = "Trade";
    private static final String contractAbi = "[{\"constant\":false,\"inputs\":[{\"name\":\"CRId\",\"type\":\"uint256\"},{\"name\":\"_workName\",\"type\":\"string\"},{\"name\":\"_workType\",\"type\":\"string\"},{\"name\":\"_BlockAddress\",\"type\":\"string\"},{\"name\":\"_authorization\",\"type\":\"string\"},{\"name\":\"_price\",\"type\":\"uint256\"}],\"name\":\"addTrade\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]";
    private static final String transUrl = "http://8.155.45.85:5002/WeBASE-Front/trans/handle";
    private static final String ownerAddress = "0x8a0d734758639120d860d9c38a7a7b81712f2449";

    private static final String contractAddress2 ="0x7d8a2611b0b21a499e389441a3be59316a698223" ;
    private static final String contractName2 = "WorksInformation";
    private static final String contractAbi2 = "[{\"constant\":false,\"inputs\":[{\"name\":\"wId\",\"type\":\"uint256\"},{\"name\":\"worksName\",\"type\":\"string\"},{\"name\":\"worksProperty\",\"type\":\"string\"},{\"name\":\"introduce\",\"type\":\"string\"},{\"name\":\"createAddress\",\"type\":\"string\"},{\"name\":\"publishAddress\",\"type\":\"string\"}],\"name\":\"addWork\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]";
    private static OkHttpClient client = new OkHttpClient();
    private static String CommonReq(String funcName, List funcParam){
        JSONArray abiJOSN = JSON.parseArray(contractAbi);
        JSONObject data = new JSONObject();
        data.put("groupId","1");
        data.put("user",ownerAddress);
        data.put("contractName",contractName);
        //data.put("version","");
        data.put("funcName",funcName);
        data.put("funcParam",funcParam);
        data.put("contractAddress",contractAddress);
        data.put("contractAbi",abiJOSN);
        //data.put("useAes",false);
        data.put("useCns",false);
        //data.put("cnsNAme","");

        try {
            RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), data.toJSONString());
            Request request = new Request.Builder()
                    .url(transUrl)
                    .post(requestBody)
                    .build();
            final Call call = client.newCall(request);
            Response response = call.execute();
            System.out.println(data.toJSONString());
            return response.body().string();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    private static String CommonReq1(String funcName, List funcParam){
        JSONArray abiJOSN = JSON.parseArray(contractAbi2);
        JSONObject data = new JSONObject();
        data.put("groupId","1");
        data.put("user",ownerAddress);
        data.put("contractName",contractName2);
        //data.put("version","");
        data.put("funcName",funcName);
        data.put("funcParam",funcParam);
        data.put("contractAddress",contractAddress2);
        data.put("contractAbi",abiJOSN);
        //data.put("useAes",false);
        data.put("useCns",false);
        //data.put("cnsNAme","");

        try {
            RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), data.toJSONString());
            Request request = new Request.Builder()
                    .url(transUrl)
                    .post(requestBody)
                    .build();
            final Call call = client.newCall(request);
            Response response = call.execute();
            System.out.println(data.toJSONString());
            return response.body().string();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    /**
     * 写合约
     * @param funcName 合约方法名
     * @param funcParam 合约方法参数列表
     * @return 合约回执
     */
    public static JSONObject writeContract(String funcName,List funcParam){
        String result = HttpUtils.CommonReq(funcName,funcParam);
        JSONObject _obj = JSON.parseObject(result);
        return _obj;
    }
    public static JSONObject writeContract2(String funcName,List funcParam){
        String result = HttpUtils.CommonReq1(funcName,funcParam);
        JSONObject _obj = JSON.parseObject(result);
        return _obj;
    }

    /**
     * 读合约
     * @param funcName 合约方法名
     * @return JSON格式数组
     */
    public static JSONArray readContract(String funcName,List funcParam){
        String result = HttpUtils.CommonReq(funcName,funcParam);
        return JSON.parseArray(result);
    }

}
